/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.tree.icon;

import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.util.Hashtable;

import javax.swing.Icon;
import javax.swing.plaf.metal.MetalIconFactory;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.tree
 * @date 14/02/2014 - 20:42:51
 */
public class TextIcons extends MetalIconFactory.TreeLeafIcon {
	
	private static Hashtable<String, String> labels;
	
	private static final long serialVersionUID = -5575152914264691485L;
	
	protected String label;
	
	/**
	 * 
	 */
	protected TextIcons() {
	
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.plaf.metal.MetalIconFactory.FileIcon16#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
	 */
	@Override
	public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
	
		super.paintIcon(c, g, x, y);
		
		//
		if(label != null) {
			
			final FontMetrics fm = g.getFontMetrics();
			
			final int offsetX = (getIconWidth() - fm.stringWidth(label)) / 2;
			final int offsetY = (getIconHeight() - fm.getHeight()) / 2 - 2;
			
			g.drawString(label, x + offsetX, y + offsetY + fm.getHeight());
		}
	}
	
	/**
	 * 
	 */
	private static void setDefaultSet() {
	
		// File Types
		labels.put("c", "C");
		labels.put("java", "J");
		labels.put("htm", "H");
		labels.put("html", "H");
		labels.put("rar", "R");
		labels.put("txt", "T");
		labels.put("zip", "Z");
	}
	
	/**
	 * @param iconLabel
	 * @return icon
	 */
	public static Icon getIcon(final String iconLabel) {
	
		//
		if(labels == null) {
			
			labels = new Hashtable<>();
			
			setDefaultSet();
		}
		
		final TextIcons icon = new TextIcons();
		
		icon.label = labels.get(iconLabel);
		
		return icon;
	}
	
	/**
	 * @param extension
	 * @param iconLabel
	 */
	public static void setLabelSet(final String extension, final String iconLabel) {
	
		//
		if(labels == null) {
			
			labels = new Hashtable<>();
			
			setDefaultSet();
		}
		
		//
		labels.put(extension, iconLabel);
	}
}
